package com.softlook.mt.client.bricks.datecontrol;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;
import com.softlook.mt.client.bricks.datecontrol.calendar.widget.simpledatepicker.DateFormatter;
import com.softlook.mt.client.bricks.datecontrol.calendar.widget.simpledatepicker.DatePicker;
import com.softlook.mt.client.events.ChangeDateEvents;
import com.softlook.mt.client.events.ChangeDateListener;

public class MTPicker extends Composite implements ChangeDateEvents {
	private DatePicker m_pickerFrom, m_pickerTo;
	private ListBox m_hrFrom, m_minFrom, m_hrTo,m_minTo; 
	private HTML m_validateLabel;
	private HashSet m_listeners = new HashSet();
	private Date m_dateBegin, m_dateEnd;
	
	public MTPicker(){
		MTLocale locale = (MTLocale) GWT.create(MTLocale.class);
		
		m_pickerFrom = new com.softlook.mt.client.bricks.datecontrol.calendar.widget.simpledatepicker.SimpleDatePicker("datePickFrom");
		m_pickerFrom.setWeekendSelectable(true);
		m_pickerFrom.setDateFormat(DateFormatter.DATE_FORMAT_MMDDYYYY);
		
		m_hrFrom = new ListBox();
		for (int i=0; i<24; i++){
			m_hrFrom.addItem(i+" "+locale.hr(),String.valueOf(i));
		}
		
		m_minFrom = new ListBox();
		for (int i=0; i<60; i++){
			m_minFrom.addItem(i+" "+locale.min(),String.valueOf(i));
		}
		
		VerticalPanel panelFrom = new VerticalPanel();
		panelFrom.add(m_pickerFrom);

		HorizontalPanel panelTimeFrom = new HorizontalPanel();
		panelTimeFrom.add(m_hrFrom);
		panelTimeFrom.add(m_minFrom);
		panelFrom.add(panelTimeFrom);
		//----------------------------
		

		m_pickerTo = new com.softlook.mt.client.bricks.datecontrol.calendar.widget.simpledatepicker.SimpleDatePicker("datePickTo");
		m_pickerTo.setWeekendSelectable(true);
		m_pickerTo.setDateFormat(DateFormatter.DATE_FORMAT_MMDDYYYY);
		
		
		m_hrTo = new ListBox();
		for (int i=0; i<24; i++){
			m_hrTo.addItem(i+" "+locale.hr(),String.valueOf(i));
		}
		
		m_minTo = new ListBox();
		for (int i=0; i<60; i++){
			m_minTo.addItem(i+" "+locale.min(),String.valueOf(i));
		}
		
		VerticalPanel panelTo = new VerticalPanel();
		panelTo.add(m_pickerTo);

		HorizontalPanel panelTimeTo = new HorizontalPanel();
		
		panelTimeTo.add(m_hrTo);
		panelTimeTo.add(m_minTo);
		panelTo.add(panelTimeTo);

		
		
		
		////////
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(new Label(locale.from())); 
		panel.add(panelFrom);
		panel.add(new Label(" "));
		panel.add(new Label(" "));
		panel.add(new Label(" "));
		panel.add(new Label(locale.to()));
		panel.add(panelTo);
		panel.add(new Label(" "));
		panel.add(new Label(" "));
		

		ClickListener tracksButtonClickListener = new ClickListener(){
			public void onClick(Widget sender) {
				validateDate();
			}
		};
		Button tracksButton = new Button(locale.tracksButton(),tracksButtonClickListener);
		tracksButton.setStyleName("curButtonStop");
		panel.add(tracksButton);
		panel.setCellVerticalAlignment(tracksButton,HasVerticalAlignment.ALIGN_MIDDLE);
		
		VerticalPanel validPanel = new VerticalPanel();
		validPanel.add(panel);
		validPanel.add(new Label(" "));
		
		m_validateLabel = new HTML(); 
		validPanel.add(m_validateLabel);
		validPanel.setCellHorizontalAlignment(m_validateLabel, HasHorizontalAlignment.ALIGN_CENTER);
		
		initWidget(validPanel);
	}
	
	private void validateDate(){
		m_dateBegin = m_pickerFrom.getSelectedDate();
		int hrFrom = Integer.parseInt(m_hrFrom.getValue(m_hrFrom.getSelectedIndex()));
		int minFrom = Integer.parseInt(m_minFrom.getValue(m_minFrom.getSelectedIndex()));
		m_dateBegin.setHours(hrFrom);
		m_dateBegin.setMinutes(minFrom);
		m_dateBegin.setSeconds(0);
		///////////////////

		m_dateEnd = m_pickerTo.getSelectedDate();
		int hrTo = Integer.parseInt(m_hrTo.getValue(m_hrTo.getSelectedIndex()));
		int minTo = Integer.parseInt(m_minTo.getValue(m_minTo.getSelectedIndex()));
		m_dateEnd.setHours(hrTo);
		m_dateEnd.setMinutes(minTo);
		m_dateEnd.setSeconds(0);

		
		MTLocaleMes locale = (MTLocaleMes) GWT.create(MTLocaleMes.class);
		m_validateLabel.setHTML(locale.fieldsFromTo(m_dateBegin.toLocaleString(), m_dateEnd.toLocaleString()));
		
		notifyListeners();
	}

	public void addChangeDateListener(ChangeDateListener listener) {
		m_listeners.add(listener);
		
	}

	public void removeChangeDateListener(ChangeDateListener listener) {
		m_listeners.remove(listener);
	}

	private void notifyListeners(){
		Iterator listenerIterator = m_listeners.iterator();
		while(listenerIterator.hasNext()){
			((ChangeDateListener)listenerIterator.next()).onChangeDate(m_dateBegin, m_dateEnd);
		}
	}
}
